package cj.web.marriage.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.dao.UserInfoDAO;
import cj.web.admin.domain.RoleInfo;
import cj.web.admin.domain.UserInfo;
import cj.web.admin.domain.UserRoleRef;
import cj.web.marriage.dao.RefAddressAuthoDAO;
import cj.web.marriage.dao.RefAddressDAO;
import cj.web.marriage.domain.RefAddress;
import cj.web.marriage.domain.RefAddressAutho;

@Path("/refAddress")
public class RefAddressController{

	private static Logger logger = Logger.getLogger(RefAddressController.class.getName());
	
	@Autowired
	private RefAddressDAO refAddressDao;
	
	@Autowired
	private RefAddressAuthoDAO refAddressAuthoDao;
	
	@Autowired
	private UserInfoDAO userInfoDao;
	
	@Get("show")
	public String show(Model model,RefAddress refAddress) {
	
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriageqyqx);
		
		return "/views/marriage/refAddressList.vm";
	}

	@Post("getList")
	public String queryList(RefAddress refAddress) {
	
		JSONObject jo = new JSONObject();
		List<RefAddress> objs = refAddressDao.query(refAddress);
		List<UserInfo> userInfolis = userInfoDao.queryAll();
		
		for(RefAddress obj:objs)
		{
			if(obj.getAuthoDescs()!=null && !"".equals(obj.getAuthoDescs()))
			{
				String[] authoDesclis = obj.getAuthoDescs().split(",");
				String authoDesc = "";
				for(int i=0;i<authoDesclis.length;i++)
				{
					for(UserInfo user:userInfolis)
					{
						if(authoDesclis[i].toString().equals(String.valueOf(user.getId()))){
							authoDesc+=user.getName()+"   ";
						}
					}
				}
				obj.setAuthoDescs(authoDesc);
			}
			
		}
		
		int size = refAddressDao.querysize(refAddress);
		jo.put("data", objs);
		jo.put("draw", refAddress.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		RefAddress obj = refAddressDao.queryById(id);
		
		List<RefAddressAutho> refAddressAutholis = refAddressAuthoDao.queryByAddressId(id);
		
		String userIds = "";
		
		for(RefAddressAutho raa:refAddressAutholis)
		{
			userIds+=raa.getUserId()+",";
		}
		
		model.add("userIds", userIds);
		model.add("refAddress", obj);
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriageqyqx);
        
		return "/views/marriage/refAddressEdit.vm";
	}
	
	@Post("refAddressJson")
	public String refAddressJson(@Param("userIds") String userIds)
	{
		JSONObject jo = new JSONObject();
		List<UserInfo> userInfolis = userInfoDao.queryAll();
		List<Map> reps = new ArrayList<Map>();
		for(UserInfo obj:userInfolis)
		{
			Map rep = new HashMap();
			rep.put("id", obj.getId());
			rep.put("pId", 0);
			rep.put("name", obj.getName());
			rep.put("open", true);
			if(userIds!=null)
			{
				rep.put("checked", (","+userIds).indexOf(","+obj.getId()+",")!=-1?true:false);
			}
			else
			{
				rep.put("checked", false);
			}
			
			reps.add(rep);
		}
		jo.put("reps", reps);
		return "@"+jo.toString();
	}
	
	@Post("update")
	public String update(Model model,@Param("id") String id,@Param("userIds") String userIds,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			refAddressAuthoDao.deleteByAddressId(Integer.parseInt(id));
			
			String[] userIdlis = userIds.split(",");
			for(int i=0;i<userIdlis.length;i++)
			{
				refAddressAuthoDao.create(Integer.parseInt(id), Integer.parseInt(userIdlis[i]));
			}
			refAddressDao.update(Integer.parseInt(id), userIds);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
}
